#!/usr/bin/env python
# -*- coding: utf-8 -*-
# tab separated value

import dsv
import sys
import locale
import codecs
import unittest

class TestSequenceFunctions(unittest.TestCase) :

    def setUp(self) :
        self.line = dsv.DSV(['album_id',
                             'album_title',
                             'album_link',
                             'album_thumbnail',
                             'photo_id',
                             'photo_title',
                             'photo_link',
                             'photo_thumbnail'])
        self.filename = 'test_tsv.txt'
        self.console_encode = locale.getpreferredencoding()

    def testexport(self) :
        self.line.fixed['album_id'] = u'album_id_1'
        self.line.fixed['album_title'] = u'アルバム 1'
        self.line.fixed['album_link'] = u'http://www.google.com'
        self.line.fixed['album_thumbnail'] = u'http://www.google.com'
        self.line.fixed['photo_id'] = u'photo_id_1'
        self.line.fixed['photo_title'] = u'写真 1'
        self.line.fixed['photo_link'] = u'http://www.google.com'
        self.line.fixed['photo_thumbnail'] ='uhttp://www.google.co.jp'
        self.line.var.append(u'')
        self.line.var.append(u'タグ1')
        self.line.var.append(u'等身大フィギュア')
        self.line.var.append(u'')
        self.line.var.append(u'タグ3')
        fd = codecs.open(self.filename, 'wb', encoding='utf-8')
        list = self.line.getList()
        s = (u'\t'.join(list))+u'\n'
        fd.write(s)
        fd.close()

    def testimport(self) :
        fd = codecs.open(self.filename, 'rb', encoding='utf-8')
        buf = fd.readline().rstrip(u'\r\n')
        self.line.split(buf)
        self.assertEqual(self.line.fixed['album_id'], u'album_id_1')
        self.assertEqual(self.line.fixed['album_id'], u'album_id_1')
        self.assertEqual(self.line.fixed['album_title'], u'アルバム 1')
        self.assertEqual(self.line.fixed['album_link'], u'http://www.google.com')
        self.assertEqual(self.line.fixed['album_thumbnail'], u'http://www.google.com')
        self.assertEqual(self.line.fixed['photo_id'], u'photo_id_1')
        self.assertEqual(self.line.fixed['photo_title'], u'写真 1')
        self.assertEqual(self.line.fixed['photo_link'], u'http://www.google.com')
        self.assertEqual(self.line.fixed['photo_thumbnail'],'uhttp://www.google.co.jp')
        self.assertEqual(len(self.line.var), 5)

if __name__ == "__main__" :
    unittest.main()
